//
//  RVideoClassifyView.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/26.
//

#import "RVideoClassifyView.h"
#import "RVideoItemView.h"
#import "RVideoObj.h"
#import "RHotClassifyRequest.h"

#import "RVideoClassifyViewController.h"

@interface RVideoClassifyView ()

/// title
@property(nonatomic,strong)MyRelativeLayout* titleLayout;

@property(nonatomic,weak)UIImageView* iconView;

@property(nonatomic,weak)UILabel* titleLb;

@property(nonatomic,weak)UIButton* moreBtn;

/// 影片
@property(nonatomic,strong)MyFlowLayout* flowLayout;

@end

@implementation RVideoClassifyView

- (instancetype)init{
    if(self = [super initWithOrientation:(MyOrientation_Vert)]){
        
        self.myHeight = MyLayoutSize.wrap;
        self.backgroundColor = UIColor.whiteColor;
        self.subviewVSpace = 8.0f;
        [self initSubViews];
    }
    return self;
}


- (void)initSubViews{
    
    MyRelativeLayout* titleLayout = [MyRelativeLayout new];
    titleLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    titleLayout.myHeight = PX(24);
    titleLayout.myHorzMargin = 0;
    [self addSubview:titleLayout];
    
    UIImageView* iconView = [[UIImageView alloc] init];
    iconView.myLeft = 0;
    iconView.myCenterY = 0;
    [titleLayout addSubview:iconView];
    _iconView = iconView;
    
    UILabel* titleLb = [UILabel lableWithFont:FONTBOLD_FIT(18) color:COLOR_BLACK_DARK text:@"--"];
    titleLb.leftPos.equalTo(iconView.rightPos).offset(6);
    titleLb.myCenterY = 0;
    [titleLayout addSubview:titleLb];
    _titleLb = titleLb;
    
    UIButton* moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn setTitleColor:COLOR_GRAY_BASE forState:UIControlStateNormal];
    [moreBtn setTitle:@"共0部＞" forState:UIControlStateNormal];
    moreBtn.titleLabel.font = FONT_FIT(12);
    [moreBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    moreBtn.contentEdgeInsets = UIEdgeInsetsMake(CGFLOAT_MIN, CGFLOAT_MIN, CGFLOAT_MIN, CGFLOAT_MIN);
    moreBtn.myRight = 0;
    moreBtn.myCenterY = 0;
    [moreBtn sizeToFit];
    [titleLayout addSubview:moreBtn];
    _moreBtn = moreBtn;
    
    // 流式布局
    MyFlowLayout *flowLayout = [MyFlowLayout flowLayoutWithOrientation:MyOrientation_Vert arrangedCount:0];
    flowLayout.myHeight = MyLayoutSize.wrap;
    flowLayout.subviewVSpace = 10;
    flowLayout.subviewHSpace = 10;
    flowLayout.gravity = MyGravity_Horz_Fill;
    flowLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    flowLayout.myMargin = 0;
    [flowLayout setSubviewsSize:PX(105) minSpace:8 maxSpace:CGFLOAT_MAX centered:NO];
    [self addSubview:flowLayout];
    _flowLayout = flowLayout;
}

- (void)setHotData:(RVideoClassify *)hotData{
    _hotData = hotData;
    
    NSString* imageName;
    NSString* title;
    switch (hotData.channel_id) {
        case VideoType_Hot:{
            imageName = @"home_recommond";
        }
            break;
        case VideoType_Movie:{
            imageName = @"home_movie";
        }
            break;
        case VideoType_TV:{
            imageName = @"home_TV";
        }
            break;
        case VideoType_Variety:{
            imageName = @"home_variety";
        }
            break;
        case VideoType_Anime:{
            imageName = @"home_anime";
        }
            break;
        case VideoType_KorTV:{
            imageName = @"home_korTV";
        }
            break;
        case VideoType_USTV:{
            imageName = @"home_usTV";
        }
            break;
        case VideoType_JapTV:{
            imageName = @"home_japTV";
        }
            break;
        default:
            break;
    }
        
    self.iconView.image = [UIImage imageNamed:imageName];
    [self.iconView sizeToFit];
    
    self.titleLb.text = hotData.c_name;
    [self.titleLb sizeToFit];
    
    [self.moreBtn setTitle:[NSString stringWithFormat:@"共%ld部＞",hotData.c_num] forState:UIControlStateNormal];
    [self.moreBtn sizeToFit];
        
    for (int i = 0; i<hotData.c_data.count; i++) {
        RVideoItemView* item = [RVideoItemView new];
        item.movie = hotData.c_data[i];
        [self.flowLayout addSubview:item];
        if(i > 5) break;
    }
}


/// 等多
/// @param sender -
- (void)moreBtnClick:(UIButton*)sender{
    
    RVideoClassifyViewController* controller = [RVideoClassifyViewController new];
    controller.classify = self.hotData;
    [UIViewController showViewController:controller animated:YES];
}

@end
